﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Cosmetic;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Collections.Generic;

namespace WebApp.Shopping
{
    public partial class orderconfirmation : PageBase
    {
        AddressManager addressmanger = new AddressManager();
        private Order m_Order;
        PurchaseManager thePurchaseManager = new PurchaseManager();
        ProductManager theProductManager = new ProductManager();
        SaleManager m_salemanager = new SaleManager();
        OrderManager theOrderManger = new OrderManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //this.PlaceHolder2.Visible = false;
                //this.LinkButton2.Visible = false;
                //this.PlaceHolder4.Visible = false;
                //this.LinkButton5.Visible = false;
                //this.PlaceHolder5.Visible = false;
                //this.PlaceHolder6.Visible = false;
               // BindProvince();
                // this.Literal10.Text = "支付宝";
                m_Order = (Order)Session["Order"];
                Literal14.Text = Convert.ToString(m_Order.Price);//商品总价
                
               

                BindCart();
                //string a = Convert.ToString(Session["userid"]);
                /*如果不为空显示收货信息 */
                if (Session["adrress"] != null)
                {
                    Address m_Order1 = (Address)Session["adrress"];
                    //this.Literal1.Text = m_Order1.Province;
                    //this.Literal2.Text = m_Order1.Consignee;
                    //this.Literal3.Text = m_Order1.Address1;
                    //this.Literal4.Text = m_Order1.Area;
                    //this.Literal5.Text = m_Order1.Phone;
                    //this.Literal6.Text = m_Order1.Mobile;
                    this.txtReceiverName.Text = m_Order1.name;
                    this.txtCellPhoneNo.Text = m_Order1.Mobile;
                    this.txtAddress.Text = m_Order1.Address1;
                    this.txtPhoneNo.Text = "";
                    this.txtPostCode.Text = m_Order1.Area;
                    //if (DDLProvince.Items.Count == 0)
                    //{ 
                    //}
                    //else
                    //{
                    //    this.DDLProvince.SelectedItem.Text = m_Order1.Province;
                    //    this.Literal7.Text = m_Order1.Province;
                    //    if (this.DDLCity.Text != "请选择……")
                    //    {
                    //        DataTable dtc = CityManager.SelectAllCityByProvinceid(DDLProvince.SelectedValue); //.SelectAllCity();

                    //        this.DDLCity.Items.Clear();
                    //        this.DDLCity.Items.Add("请选择……");
                    //        for (int i = 0; i < dtc.Rows.Count; i++)
                    //        {
                    //            ListItem li = new ListItem(dtc.Rows[i]["city"].ToString(), dtc.Rows[i]["cityid"].ToString());
                    //            DDLCity.Items.Add(li);
                    //        }
                    //        this.DDLCity.SelectedItem.Text =  m_Order1 .City ;
                    //        this.Literal8.Text = m_Order1.City;
                    //    }
                    //}
                    //if (DDLCity.Items.Count == 0)
                    //{
                    //}
                    //else
                    //{
                    //    if (this.DDLCity.Text != "请选择……")
                    //    {
                    //        DataTable dta = CityManager.SelectAllAreaByCityid(DDLCity.SelectedValue);
                    //        this.ddlArea.Items.Clear();
                    //        this.ddlArea.Items.Add("请选择……");
                    //        for (int i = 0; i < dta.Rows.Count; i++)
                    //        {
                    //            ListItem li = new ListItem(dta.Rows[i]["area"].ToString(), dta.Rows[i]["areaid"].ToString());
                    //            ddlArea.Items.Add(li);
                    //        }
                    //        this.ddlArea.SelectedItem.Text =  m_Order1 .Area;
                    //        this.Literal9.Text = m_Order1.Area;
                    //    }
                    //}
                    string city = m_Order1.Province;
                    // this.Literal12.Text = GetDeliveryPrive(city).ToString(); //运费
                    //this.lbDeliveryPrice2.Text = GetDeliveryPrive(city).ToString(); //配送费用
                    // this.lbTotalPrice.Text = Convert.ToString(m_Order.Price);// Convert.ToString(m_Order.Price + GetDeliveryPrive(city));//加运费的总价钱。
                    // this.lbAfterAllPrice.Text = Convert.ToString(m_Order.Price);//使用优惠券打折后商品总价
                    Cosmetic.DataEntity.Address theUser = this.addressmanger.LoginValidate(m_Order1.UserId);
                    if (theUser != null)
                    {
                        //this.lvActivity.DataSource = addressmanger.GetAddress(m_Order1.UserId);
                        //this.lvActivity.DataBind();
                    }
                }
                else
                {
                    /* 如果Session["adrress"]为空但此用户订单不为空 */
                    string a = Convert.ToString(Session["userid"]);
                    if (theOrderManger.CheckExistemail(new Guid(a)) != null)
                    {
                        Cosmetic.DataEntity.Order theUser = this.theOrderManger.CheckExistemail(new Guid(a));
                        this.txtReceiverName.Text = theUser.DeliveryUserName;
                        //if (DDLProvince.Items.Count == 0)
                        //{
                        //    //this.DDLProvince.Text = AddressManager.GetCityName(adds[num].Province);
                        //}
                        //else
                        //{
                        //    this.DDLProvince.SelectedItem.Text = theUser.adress1;
                        //    this.Literal7.Text = theUser.adress1;
                        //    string city = theUser.adress1;
                        //    this.Literal12.Text = GetDeliveryPrive(city).ToString();
                        //    this.lbDeliveryPrice2.Text = GetDeliveryPrive(city).ToString();
                        //    this.lbTotalPrice.Text = Convert.ToString(m_Order.Price + GetDeliveryPrive(city));
                        //    this.lbAfterAllPrice.Text = Convert.ToString(m_Order.Price);
                        //    if (this.DDLCity.Text != "请选择……")
                        //    {
                        //        DataTable dtc = CityManager.SelectAllCityByProvinceid(DDLProvince.SelectedValue); //.SelectAllCity();
                        //        this.DDLCity.Items.Clear();
                        //        this.DDLCity.Items.Add("请选择……");
                        //        for (int i = 0; i < dtc.Rows.Count; i++)
                        //        {
                        //            ListItem li = new ListItem(dtc.Rows[i]["city"].ToString(), dtc.Rows[i]["cityid"].ToString());
                        //            DDLCity.Items.Add(li);
                        //        }
                        //        this.DDLCity.SelectedItem.Text = theUser.adress2;
                        //        this.Literal8.Text = theUser.adress2;
                        //    }
                        //}
                        //if (DDLCity.Items.Count == 0)
                        //{
                        //}
                        //else
                        //{
                        //    if (this.DDLCity.Text != "请选择……")
                        //    {
                        //        DataTable dta = CityManager.SelectAllAreaByCityid(DDLCity.SelectedValue);
                        //        this.ddlArea.Items.Clear();
                        //        this.ddlArea.Items.Add("请选择……");
                        //        for (int i = 0; i < dta.Rows.Count; i++)
                        //        {
                        //            ListItem li = new ListItem(dta.Rows[i]["area"].ToString(), dta.Rows[i]["areaid"].ToString());
                        //            ddlArea.Items.Add(li);
                        //        }
                        //        this.ddlArea.SelectedItem.Text = theUser.adress3;
                        //        this.Literal9.Text = theUser.adress3;
                        //    }
                        //}
                        this.txtAddress.Text = theUser.adress4;
                        this.txtPhoneNo.Text = theUser.User.Email;
                        this.txtPostCode.Text = theUser.DeliveryPostCode;
                        this.txtCellPhoneNo.Text = theUser.DeliveryCellPhone;
                        this.txtReceiverName.Text = theUser.DeliveryUserName;
                    }
                    Cosmetic.DataEntity.Address theUser1 = this.addressmanger.LoginValidate(this.SignInUser.UserId);
                    if (theUser1 != null)
                    {
                        //this.lvActivity.DataSource = addressmanger.GetAddress(this.SignInUser.UserId);
                        //this.lvActivity.DataBind();
                        //this.Literal1.Text = theUser.Province;
                        //this.Literal2.Text = theUser.Consignee;
                        //this.Literal3.Text = theUser.Address1;
                        //this.Literal4.Text = theUser.Area;
                        //this.Literal5.Text = theUser.Phone;
                        //this.Literal6.Text = theUser.Mobile;
                        this.txtReceiverName.Text = theUser1.name;
                        this.txtCellPhoneNo.Text = theUser1.Mobile;
                        this.txtAddress.Text = theUser1.Address1;
                        this.txtPhoneNo.Text = "";
                        this.txtPostCode.Text = theUser1.Area;
                        //if (DDLProvince.Items.Count == 0)
                        //{
                        //}
                        //else
                        //{
                        //    this.DDLProvince.SelectedItem.Text = theUser1.Province;
                        //    this.Literal7.Text = theUser1.Province;
                        //    if (this.DDLCity.Text != "请选择……")
                        //    {
                        //        DataTable dtc = CityManager.SelectAllCityByProvinceid(DDLProvince.SelectedValue); //.SelectAllCity();
                        //        this.DDLCity.Items.Clear();
                        //        this.DDLCity.Items.Add("请选择……");
                        //        for (int i = 0; i < dtc.Rows.Count; i++)
                        //        {
                        //            ListItem li = new ListItem(dtc.Rows[i]["city"].ToString(), dtc.Rows[i]["cityid"].ToString());
                        //            DDLCity.Items.Add(li);
                        //        }
                        //        this.DDLCity.SelectedItem.Text = theUser1.City;
                        //        this.Literal8.Text = theUser1.City;
                        //    }
                        //}
                        //if (DDLCity.Items.Count == 0)
                        //{
                        //}
                        //else
                        //{
                        //    //if (this.DDLCity.Text != "请选择……")
                        //    //{
                        //    //    DataTable dta = CityManager.SelectAllAreaByCityid(DDLCity.SelectedValue);
                        //    //    this.ddlArea.Items.Clear();
                        //    //    this.ddlArea.Items.Add("请选择……");
                        //    //    for (int i = 0; i < dta.Rows.Count; i++)
                        //    //    {
                        //    //        ListItem li = new ListItem(dta.Rows[i]["area"].ToString(), dta.Rows[i]["areaid"].ToString());
                        //    //        ddlArea.Items.Add(li);
                        //    //    }
                        //    //    this.ddlArea.SelectedItem.Text = theUser1.Area;
                        //    //    this.Literal9.Text = theUser1.Area;
                        //    //}
                        //}
                        string city = theUser1.Province;
                        //this.Literal12.Text = GetDeliveryPrive(city).ToString();
                        //this.lbDeliveryPrice2.Text = GetDeliveryPrive(city).ToString();
                        // this.lbTotalPrice.Text = Convert.ToString(m_Order.Price + GetDeliveryPrive(city));
                        //this.lbAfterAllPrice.Text = Convert.ToString(m_Order.Price);
                    }
                }
                if (this.txtAddress.Text.Contains(System.Configuration.ConfigurationManager.AppSettings["cityName"].ToString()))
                {
                    this.litCarriage.Text = System.Configuration.ConfigurationManager.AppSettings["shanghai"].ToString();
                }
                else
                {
                    this.litCarriage.Text = System.Configuration.ConfigurationManager.AppSettings["otherCity"].ToString();
                }

                int carriage = 0;
                int.TryParse(litCarriage.Text, out carriage);

                lbTotalPrice.Text = Convert.ToString(m_Order.Price + carriage);//商品总价
                lbIntegral.Text = string.Format("{0:F0}", m_Order.Price + carriage);
            }
        }




        //protected void LinkButton1_Click(object sender, EventArgs e)
        //{
        //    if (this.LinkButton1.Text == "[关闭]")
        //    {

        //            this.PlaceHolder1.Visible = false;
        //            this.PlaceHolder2.Visible = true;
        //            this.LinkButton2.Visible = true;
        //            this.LinkButton2.Text = "[修改]";
        //            this.LinkButton1.Visible = false;
        //            this.Literal1.Text = this.txtReceiverName.Text;
        //            this.Literal2.Text = this.DDLProvince.SelectedItem.Text + this.DDLCity.SelectedItem.Text + this.ddlArea.SelectedItem.Text;
        //            this.Literal3.Text = this.txtAddress.Text;
        //            this.Literal4.Text = txtPostCode.Text;
        //            this.Literal5.Text = this.txtCellPhoneNo.Text;
        //            this.Literal6.Text = this.txtPhoneNo.Text;


        //    }

        //}

        //protected void LinkButton2_Click(object sender, EventArgs e)
        //{
        //    if (this.LinkButton2.Text == "[修改]")
        //    {
        //        this.PlaceHolder1.Visible = true;
        //        this.PlaceHolder2.Visible = false;
        //        this.LinkButton2.Visible = false;
        //        this.LinkButton1.Visible = true;

        //    }

        //}

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            //if (this.DDLProvince.Text == "请选择……")
            //{
            //    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('省份地址不完整');", true);
            //    return;
            //}
            if (string.IsNullOrEmpty(this.txtPhoneNo.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请正邮箱!')", true);
                return;
            }
            if (this.txtReceiverName.Text == null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写收货人姓名!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtAddress.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写收货地址!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtPostCode.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写邮编!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtCellPhoneNo.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写手机电话!')", true);
                return;
            }
            //if (this.DDLCity.Text == "请选择……")
            //{
            //    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('省份地址不完整');", true);
            //    return;
            //}
            //if (this.ddlArea.Text == "请选择……")
            //{
            //    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('省份地址不完整');", true);
            //    return;
            //}
            /* 添加地址 */
            Address address = new Address();
            address.AddressId = Guid.NewGuid();
            address.Mark = false;
            // address.Consignee = this.DDLProvince.SelectedItem.Text + this.DDLCity.SelectedItem.Text + this.ddlArea.SelectedItem.Text;
            address.UserId = new Guid(Session["userid"].ToString());
            address.Phone = this.txtCellPhoneNo.Text;
            address.Mobile = this.txtPhoneNo.Text.Trim();
            address.Address1 = this.txtAddress.Text;
            address.Area = this.txtPostCode.Text;
            // address.Province = this.DDLProvince.SelectedItem.Text;
            address.name = this.txtReceiverName.Text;
            // address.zip = this.txtReceiverName.Text + "," + this.DDLProvince.SelectedItem.Text + this.DDLCity.SelectedItem.Text + this.ddlArea.SelectedItem.Text + "," + this.txtAddress.Text;
            // address.City = this.DDLCity.SelectedItem.Text;
            // address.District = this.ddlArea.SelectedItem.Text;
            Session["adrress"] = address;
            addressmanger.Insert(address);
            Address m_Order1 = (Address)Session["adrress"];
            Address theaddress = addressmanger.GetAddressById(m_Order1.AddressId);
            theaddress.Mark = true;
            addressmanger.Update();
            //this.lvActivity.DataSource = addressmanger.GetAddress(m_Order1.UserId);
            //this.lvActivity.DataBind();

        }
        protected void lvActivity_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            /*删除地址并刷新 */
            Address theaddress = addressmanger.GetAddressById(new Guid(e.CommandArgument.ToString()));

            if (e.CommandName.Equals("Onjihuo"))
            {
                this.addressmanger.Delete(theaddress);
                Address m_Order1 = (Address)Session["adrress"];
                //this.lvActivity.DataSource = addressmanger.GetAddress(m_Order1.UserId);
                //this.lvActivity.DataBind();
            }
        }




        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {

            ///*RadioButton 设置为单选 */
            //RadioButton senderRb = sender as RadioButton;
            //foreach (ListViewDataItem item in this.lvActivity.Items)
            //{
            //    RadioButton rb = item.FindControl("RadioButton1") as RadioButton;
            //    rb.Checked = false;
            //}
            //senderRb.Checked = true;
            ///* 选择时显示 */
            //foreach (ListViewDataItem item in this.lvActivity.Items)
            //{
            //    if ((item.FindControl("RadioButton1") as RadioButton).Checked == true)
            //    {
            //        string pid = (item.FindControl("HFPid") as HiddenField).Value;
            //        Address theaddress1 = addressmanger.GetAddressById(new Guid(pid));
            //        this.txtReceiverName.Text = theaddress1.name;
            //        this.DDLProvince.SelectedItem.Text = theaddress1.Province;
            //        this.DDLCity.SelectedItem.Text = theaddress1.City;
            //        this.ddlArea.SelectedItem.Text = theaddress1.District;
            //        //this.Literal7.Text = theaddress1.Province;
            //        //this.Literal8.Text = theaddress1.City;
            //        //this.Literal9.Text = theaddress1.District;
            //        this.txtAddress.Text = theaddress1.Address1;
            //        this.txtPhoneNo.Text = theaddress1.Mobile;
            //        this.txtPostCode.Text = theaddress1.Area;
            //        this.txtCellPhoneNo.Text = theaddress1.Phone;
            //    }

            //}

        }

        /// <summary>
        /// 绑定省。
        /// </summary>
        private void BindProvince()
        {
            //DataTable dt = CityManager.SelectAllProvinces();
            //this.DDLProvince.Items.Clear();
            //this.DDLProvince.Items.Add("请选择……");
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{

            //    ListItem li = new ListItem(dt.Rows[i]["province"].ToString(), dt.Rows[i]["provinceid"].ToString());
            //    DDLProvince.Items.Add(li);
            //}
        }

        protected void DDLProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataTable dt = CityManager.SelectAllCityByProvinceid(DDLProvince.SelectedValue);
            //this.DDLCity.Items.Clear();
            //this.DDLCity.Items.Add("请选择……");
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    ListItem li = new ListItem(dt.Rows[i]["city"].ToString(), dt.Rows[i]["cityid"].ToString());
            //    DDLCity.Items.Add(li);
            //}
            //this.ddlArea.Items.Clear();
            //this.ddlArea.Items.Add("请选择……");
            //m_Order = (Order)Session["Order"];
            //this.Literal7.Text = this.DDLProvince.SelectedItem.Text;
            //string city = this.DDLProvince.SelectedItem.Text;
            //this.Literal12.Text = GetDeliveryPrive(city).ToString();
            //this.lbDeliveryPrice2.Text = GetDeliveryPrive(city).ToString();
            //this.lbTotalPrice.Text = Convert.ToString(m_Order.Price + GetDeliveryPrive(city));
            //this.lbAfterAllPrice.Text = Convert.ToString(m_Order.Price);
        }
        /* 运费*/

        //private decimal GetDeliveryPrive(string area)
        //{
        //    int weight = GetProductsWeight();

        //    decimal deliveryPrice = theProductManager.GetDeliveryPriceByWeightAndArea(weight, area);

        //    return deliveryPrice;
        //}

        private int GetProductsWeight()
        {
            int totalweight = 0;
            if (Session["buycart"] != null)
            {
                Hashtable ht = Session["buycart"] as Hashtable;
                IList products = theProductManager.GetProducts_Cos(ht);
                int a = Convert.ToInt32(Session["count"]);
                foreach (var item in products)
                {
                    totalweight = (Convert.ToInt32(item.GetType().GetProperty("ProductWeight").GetValue(item, null)) * a);
                }
            }
            return totalweight;
        }

        protected void DDLCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataTable dt = CityManager.SelectAllAreaByCityid(DDLCity.SelectedValue);

            //this.ddlArea.Items.Clear();
            //this.ddlArea.Items.Add("请选择……");
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{

            //    ListItem li = new ListItem(dt.Rows[i]["area"].ToString(), dt.Rows[i]["areaid"].ToString());
            //    ddlArea.Items.Add(li);
            //}
            //this.Literal8.Text = this.DDLCity.SelectedItem.Text;
        }

        protected void ddlArea_SelectedIndexChanged(object sender, EventArgs e)
        {

            // this.Literal9.Text = this.ddlArea.SelectedItem.Text;
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            //if (this.LinkButton4.Text == "[修改]")
            //{
            //    this.PlaceHolder4.Visible = true;
            //    this.PlaceHolder3.Visible = false;
            //    this.LinkButton5.Visible = true;
            //    this.LinkButton4.Visible = false;
            //}

        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            //if (this.LinkButton5.Text == "[关闭]")
            //{
            //    this.PlaceHolder4.Visible = false;
            //    this.PlaceHolder3.Visible = true;
            //    this.LinkButton5.Visible = false;
            //    this.LinkButton4.Visible = true;
            //}
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            //this.PlaceHolder3.Visible = true;
            //this.PlaceHolder4.Visible = false;
            //this.LinkButton5.Visible = false;
            //this.LinkButton4.Visible = true;
            //if (!this.CBAliPay.Checked && !this.CBCOD.Checked && !this.CheckBox1.Checked)
            //{
            //    Page.RegisterStartupScript("openalert", "<script>alert('请选择支付方式');</script>");

            //    return;
            //}
            //if (this.CBAliPay.Checked)
            //{
            //    this.Literal10.Text = "支付宝";
            //}

            //if (this.CheckBox1.Checked)
            //{
            //    this.Literal10.Text = "网汇通";
            //}
            //else if (this.CBCOD.Checked)
            //{
            //    this.Literal10.Text = "商付宝";
            //}
        }
        /*使用优惠券 */
        protected void Button3_Click(object sender, EventArgs e)
        {
            //if (this.TextBox2.Text == null)
            //{
            //    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请输入优惠券卡号!');", true);
            //    return;
            //}
            //Cosmetic.DataEntity.youhui theUser = this.m_salemanager.CheckExistes(this.TextBox2.Text.Trim());
            //if (null == theUser)
            //{
            //    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('输入的卡号错误!');", true);
            //    return;
            //}
            //else
            //{
            //    m_Order = (Order)Session["Order"];
            //    youhui theyouhui = m_salemanager.getyouhui(this.TextBox2.Text);
            //    if (theyouhui.value == true)
            //    {
            //        this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('此优惠券已经用过!');", true);
            //        return;
            //    }
            //    m_Order.Price = m_Order.Price * (m_salemanager.GetDiscountRatiof1(this.TextBox2.Text));
            //    Session["Order"] = m_Order;
            //    Session["ordernum"] = this.TextBox2.Text;
            //    this.Literal13.Text = Convert.ToString(m_Order.Price);
            //    PlaceHolder5.Visible = true;
            //    Literal1.Text = Convert.ToString(m_Order.Price);
            //    theyouhui.userid = this.SignInUser.UserId;
            //    m_salemanager.Update();
            //    PlaceHolder6.Visible = true;
            //    this.lbAfterAllPrice.Text = Convert.ToString(m_Order.Price);
            //    string city = this.DDLProvince.SelectedItem.Text;
            //    this.lbTotalPrice.Text = Convert.ToString(m_Order.Price + GetDeliveryPrive(city));
            //    this.Button3.Enabled = false;
            //    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('恭喜您使用优惠券成功，打折后商品的总价格为" + m_Order.Price + "!');", true);
            //    return;

            //}

        }

        private void BindCart()
        {
            if (Session["buycart"] != null && (Session["buycart"] as Hashtable).Count != 0)
            {
                this.lvProductnew.DataSource = theProductManager.GetProducts_Cos((Hashtable)Session["buycart"]);
                this.lvProductnew.DataBind();
            }

            if (Session["DiscountCart"] != null && (Session["DiscountCart"] as Hashtable).Count != 0)
            {
                this.ListView_Dis.DataSource = theProductManager.GetProducts_RH((Hashtable)Session["DiscountCart"]);
                this.ListView_Dis.DataBind();
            }
        }

        protected void lvProductnew_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (Session["buycart"] != null && (Session["buycart"] as Hashtable).Count != 0)
            {
                Hashtable ht = Session["buycart"] as Hashtable;

                string pid = (e.Item.FindControl("HFPid") as HiddenField).Value;
                Label lbcount = e.Item.FindControl("lbCount") as Label;

                if (lbcount != null)
                {
                    lbcount.Text = ht[pid].ToString();
                }

                //计算小计价格
                Literal lblSmallCount = e.Item.FindControl("SmallCountNew") as Literal;

                decimal uintprice = decimal.Parse((e.Item.FindControl("UnitPrice") as Literal).Text);
                int count = Convert.ToInt32(ht[pid].ToString());
                if (m_salemanager.CheckSaleProduct(pid) && count >= 2)
                {
                    lblSmallCount.Text = (uintprice * (count / 2) * 2 * m_salemanager.GetDiscountRatio() + (count % 2) * uintprice).ToString("F2");
                }
                else
                {
                    lblSmallCount.Text = (uintprice * count).ToString("F2");
                }
                //(e.Item.FindControl("lbName") as Label).Text += "(Sale，每2件打" + m_salemanager.GetDiscountRatio() * 10 + "折)";
                //累加总价
                //this.m_Price = m_Price + uintprice * count;//).ToString("F2");

                //显示尺寸
                if (Session["size"] != null && ((Hashtable)Session["size"]).Count != 0)
                {
                    if ((Session["size"] as Hashtable).Contains(pid))
                    {
                        //Label lbsize = e.Item.FindControl("lbSize") as Label;

                        //lbsize.Text = (Session["size"] as Hashtable)[pid].ToString() ;
                    }
                }
                //Label allprice = e.Item.FindControl("lbAllPrice") as Label;
                //allprice.Text = (double.Parse(allprice.Text) + uintprice * count).ToString();
            }
            else if (Session["bookcart"] != null && (Session["bookcart"] as Hashtable).Count != 0)
            {
                Hashtable ht = Session["bookcart"] as Hashtable;

                string pid = (e.Item.FindControl("HFPid") as HiddenField).Value;

                Label lbcount = e.Item.FindControl("lbCount") as Label;

                if (lbcount != null)
                {
                    lbcount.Text = ht[pid].ToString();
                }

                //计算小计价格
                Label lblSmallCount = e.Item.FindControl("SmallCountNew") as Label;

                decimal uintprice = decimal.Parse((e.Item.FindControl("UnitPrice") as Label).Text);
                int count = Convert.ToInt32(ht[pid].ToString());

                lblSmallCount.Text = (uintprice * count).ToString("F2");


                if (Session["size"] != null && ((Hashtable)Session["size"]).Count != 0)
                {
                    if ((Session["size"] as Hashtable).Contains(pid))
                    {
                        Label lbsize = e.Item.FindControl("lbSize") as Label;
                        lbsize.Text = (Session["size"] as Hashtable)[pid].ToString();
                    }
                }
            }
        }
        /* 返回购物车修改 */
        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Shopping/shopCart.aspx");
        }
        /*  提交订单*/
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtPhoneNo.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请正邮箱!')", true);
                return;
            }
            if (this.txtReceiverName.Text == null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写收货人姓名!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtAddress.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写收货地址!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtPostCode.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写邮编!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtCellPhoneNo.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写手机电话!')", true);
                return;
            }

            m_Order = (Order)Session["Order"];

            //if (!this.CBAliPay.Checked && !this.CBCOD.Checked && !this.CheckBox1.Checked)
            //{
            //    Page.RegisterStartupScript("openalert", "<script>alert('请选择支付方式');</script>");
            //    return;
            //}

            if (this.rdbtnPayType.SelectedItem.Text == "支付宝")
            {
                m_Order.PayType = "支付宝";
                m_Order.Valid = false;
            }
            else
            {
                m_Order.PayType = "银行转账";
                m_Order.Valid = false;
            }

            //if (this.CheckBox1.Checked)
            //{
            //    m_Order.PayType = "网汇通";
            //    m_Order.Valid = false;
            //}
            //else if (this.CBCOD.Checked)
            //{
            //    m_Order.PayType = "商付宝";
            //    m_Order.Valid = false;
            //}

            Session["PayType"] = m_Order.PayType;

            Hashtable theHashtable = null;
            Hashtable packageHashtable = null;
            if (Session["buycart"] != null)
            {
                theHashtable = Session["buycart"] as Hashtable;
            }
            if (Session["DiscountCart"] != null)
            {
                packageHashtable = Session["DiscountCart"] as Hashtable;
            }
            if (Session["integral"] == null)
            {
                m_Order.Intergral = (m_Order.Price);// + Convert.ToDecimal(Session["DeliveryPrice"])
            }
            else
            {
                m_Order.Intergral = (m_Order.Price);
            }

            string city = "";// this.DDLProvince.SelectedItem.Text;
            m_Order.DeliveryType = "快递 ";
            m_Order.ExpressNum = this.ddlExpress.SelectedItem.Text;
            Session["DeliveryPrice"] = litCarriage.Text;//GetDeliveryPrive(city).ToString();
            m_Order.DeliveryUserName = this.txtReceiverName.Text.Trim();
            m_Order.DeliveryAddress = this.txtAddress.Text;// this.DDLProvince.SelectedItem.Text + this.DDLCity.SelectedItem.Text + this.txtAddress.Text.Trim();//this.DropDownList1.SelectedItem.Text+"  "+this.DropDownList2.SelectedItem.Text+"  "+this.txtDeliveryAddress.Text.Trim();
            m_Order.DeliveryPostCode = this.txtPostCode.Text.Trim();
            m_Order.DeliveryTel = this.txtPhoneNo.Text.Trim();
            m_Order.DeliveryCellPhone = Session["tel"].ToString();
            m_Order.adress1 = "";// this.DDLProvince.SelectedItem.Text;
            m_Order.adress2 = "";// this.DDLCity.SelectedItem.Text;
            m_Order.adress3 = "";// this.ddlArea.SelectedItem.Text;
            m_Order.adress4 = this.txtAddress.Text;
            m_Order.tel1 = "021";
            m_Order.tel2 = this.txtPhoneNo.Text;
            m_Order.DeliveryAddress1 = "";// this.txtAddress1.Text;
            m_Order.DeliveryAddress2 = "";// this.txtAddress2.Text;
            m_Order.DeliveryPrice = Convert.ToDecimal(Session["DeliveryPrice"]);
            m_Order.User = this.SignInUser;
            m_Order.IsBook = false;
            m_Order.UseIntegral = 0;// Convert.ToDecimal(this.lbTotalPrice.Text);
            m_Order.SendDate = DateTime.Now;
            m_Order.OrderId = Guid.NewGuid();
            m_Order.UserId = new Guid(Session["userid"].ToString());
            //生成订单
            PurchaseManager thePurchaseManager = new PurchaseManager();
            m_Order.CreateTime = DateTime.Now;
            m_Order.month = DateTime.Now.Month;
            m_Order.AddressId = new Guid("f0241e6d-b0af-4911-bc45-af3688dad592");//user.Addresses[0].AddressId;
            m_Order.State = "未付款"; //状态：未付款、已付款、已发货、申请退款、已退款、完成
            m_Order.DeliveryPrice = Convert.ToDecimal(Session["DeliveryPrice"]);
            m_Order.CompleteTime = DateTime.Now;
            m_Order.Year = DateTime.Now.Year;
            m_Order.OrderNum = thePurchaseManager.CreateOrderNum();
            m_Order.allprice = Convert.ToDecimal(this.lbTotalPrice.Text);
            m_Order.Price = Convert.ToDecimal(this.Literal14.Text);// Convert.ToDecimal(this.lbAfterAllPrice.Text);
            m_Order.Remark = "非分销商";

            Session["Order"] = null;
            Session["count"] = null;
            Session["buycart"] = null;
            Session["bookcart"] = null;
            Session["pointcart"] = null;
            Session["DiscountCart"] = null;

            if (Session["size"] == null || ((Hashtable)Session["size"]).Count == 0)
            {
                thePurchaseManager.InsertOrder(m_Order, theHashtable, packageHashtable);
            }
            else
            {
                thePurchaseManager.InsertOrder(m_Order, theHashtable, packageHashtable, (Hashtable)Session["size"]);
            }
            if (m_Order.PayType == "支付宝")
            {
                Response.Redirect(String.Format("ReturnS.aspx?TradeNo={0}", m_Order.OrderNum));
            }
            else
            {
                Response.Redirect("OrderSuccess.aspx");
            }

            //if (m_Order.PayType == "网汇通")
            //{
            //    Response.Redirect(String.Format("ReturnS1.aspx?TradeNo={0}", m_Order.OrderNum));
            //}
            //else
            //{
            //    Response.Redirect(String.Format("Return.aspx?TradeNo={0}", m_Order.OrderNum));
            //}
        }

        protected void Button1_Click(object sender, ImageClickEventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtPhoneNo.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请正邮箱!')", true);
                return;
            }
            if (this.txtReceiverName.Text == null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写收货人姓名!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtAddress.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写收货地址!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtPostCode.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写邮编!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtCellPhoneNo.Text))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('请填写手机电话!')", true);
                return;
            }
            /*   session 保存地址 */
            Address address = new Address();
            address.AddressId = Guid.NewGuid();
            address.Mark = false;
            // address.Consignee = this.DDLProvince.SelectedItem.Text + this.DDLCity.SelectedItem.Text + this.ddlArea.SelectedItem.Text;
            address.UserId = new Guid("07C684D3-910F-44FD-9781-2DEE610B59E8");
            address.Phone = this.txtCellPhoneNo.Text;
            address.Mobile = this.txtPhoneNo.Text.Trim();
            address.Address1 = this.txtAddress.Text;
            address.Area = this.txtPostCode.Text;
            //  address.Province = this.DDLProvince.SelectedItem.Text;
            address.name = this.txtReceiverName.Text;
            // address.zip = this.txtReceiverName.Text + "," + this.DDLProvince.SelectedItem.Text + this.DDLCity.SelectedItem.Text + this.ddlArea.SelectedItem.Text + "," + this.txtAddress.Text;
            // address.City = this.DDLCity.SelectedItem.Text;
            //  address.District = this.ddlArea.SelectedItem.Text;
            Session["adrress"] = address;
            //this.PlaceHolder1.Visible = false;
            //this.PlaceHolder2.Visible = true;
            //this.LinkButton2.Visible = true;
            //this.LinkButton1.Visible = false;
            Address m_Order1 = (Address)Session["adrress"];
            //this.Literal1.Text = m_Order1.name;
            //this.Literal2.Text = m_Order1.Consignee;
            //this.Literal3.Text = m_Order1.Address1;
            //this.Literal4.Text = m_Order1.Area;
            //this.Literal5.Text = m_Order1.Phone;
            //this.Literal6.Text = m_Order1.Mobile;

            if (this.txtAddress.Text.Contains(System.Configuration.ConfigurationManager.AppSettings["cityName"].ToString()))
            {
                this.litCarriage.Text = System.Configuration.ConfigurationManager.AppSettings["shanghai"].ToString();
            }
            else
            {
                this.litCarriage.Text = System.Configuration.ConfigurationManager.AppSettings["otherCity"].ToString();
            }
            this.lbTotalPrice.Text = (decimal.Parse(Literal14.Text) + decimal.Parse(litCarriage.Text)).ToString();
            lbIntegral.Text = string.Format("{0:F0}", decimal.Parse(Literal14.Text) + decimal.Parse(litCarriage.Text));
        }
    }
}





